from tkinter import *
from tkinter.messagebox import *
import threading
import socket

class chat_client():
    def __init__(self,host,port):#初始化socket对象
        self.host=host
        self.port=port
    def draw_login(self):#绘制登录窗口
        self.login_root=Tk()
        root=self.login_root
        root.title('ICHAT - 登录')
        root.geometry()
        #创建登陆框各类元素
        Label(root,text='账号').grid(row=1,column=0,padx=5,pady=5)
        Label(root,text='密码').grid(row=2,column=0,padx=5,pady=5)
        self.login_username_entry=Entry(root)
        self.login_password_entry=Entry(root)
        signup_button=Button(root,text='注册账号',width=8,height=2,command=quit)
        login_button=Button(root,text='登录',width=8,height=2,command=self.login)
        #排版
        self.login_username_entry.grid(row=1,columnspan=2,column=1,padx=5,pady=5)
        self.login_password_entry.grid(row=2,columnspan=2,column=1,padx=5,pady=5)
        signup_button.grid(row=3,column=1,padx=5,pady=5)
        login_button.grid(row=3,column=2,padx=5,pady=5)
        root.mainloop()
    def draw_chat_room(self):#绘制聊天窗口
        self.chat_room_root=Tk()
        root=self.chat_room_root
        root.title('ICHAT - 多人聊天室')
        root.geometry()
        #定义各个窗口
        self.chat_room_output_msg_text=Text(root,width=60,height=24,state=DISABLED)#聊天输出框
        self.chat_room_input_msg_text=Text(root,width=60,height=8)#输入框
        self.chat_room_friends_list=Text(root,width=20,height=38)#好友列表
        ad_left_bottom=Frame(root,width=220)#广告区域
        Message(ad_left_bottom,text='ICHAT 广告赞助区域')#广告信息
        close_chat_room_button=Button(root,text='关闭',width=10,height=2,command=self.close_chat_room)#退出按钮
        send_msg_button=Button(root,text='发送',width=10,height=2,command=self.send_msg)#发送消息按钮
        #使用grid排版会话窗口
        self.chat_room_output_msg_text.grid(row=0,column=0,columnspan=3,padx=5,pady=5)#排版聊天输出框
        self.chat_room_input_msg_text.grid(row=1,column=0,columnspan=3,padx=5,pady=5)#排版输入框
        self.chat_room_friends_list.grid(row=0,column=3,rowspan=3,padx=5,pady=5)#排版好友列表
        ad_left_bottom.grid(row=2,column=0,padx=5,pady=5)#排版广告框
        close_chat_room_button.grid(row=2,column=1,padx=5,pady=5)#排版关闭按钮
        send_msg_button.grid(row=2,column=2,padx=5,pady=5)#排版发送消息按钮
        root.mainloop()
    def warning_msg_box(self,msg):#弹出警告窗
        showwarning(title="错误信息!!!",message=msg)
    def run(self):#运行
        self.draw_login()
    def login(self):
        self.client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.get_msg_thread=threading.Thread(target=self.get_msg)
        client=self.client
        client.connect((self.host,self.port))
        username=self.login_username_entry.get()#取得用户输入的username
        password=self.login_password_entry.get()#取得用户输入的password
        print(username,password)
        client.send((username+":"+password).encode('utf-8'))#发送用户登录账户
        msg=client.recv(1024)#接收登录返回信息
        msg=msg.decode()
        print(msg)
        if msg=="login success":
            self.login_root.destroy()
            self.get_msg_thread.start()
            self.draw_chat_room()
        elif msg=="error password,disconnect!":
            self.warning_msg_box("error password,disconnect!")
        elif msg=="error username,disconnect!":
            self.warning_msg_box("error username,disconnect!")
        elif msg=="error login msg,disconnect!":
            self.warning_msg_box("error login msg,disconnect!")
        self.login_root.update()
    #发送消息
    def send_msg(self):
        msg=self.chat_room_input_msg_text.get('0.0',END)
        self.chat_room_output_msg_text['state']='normal'
        self.chat_room_output_msg_text.insert(END, msg+'\n')
        self.chat_room_output_msg_text['state']='disable'
        self.client.send(msg.encode('utf-8'))
        self.chat_room_input_msg_text.delete('0.0',END)
        self.chat_room_root.update()
    #接收消息
    def get_msg(self):
        while True:
            try:
                msg=self.client.recv(1024)
                self.chat_room_output_msg_text['state']='normal'
                self.chat_room_output_msg_text.insert(END, msg.decode()+'\n')
                self.chat_room_output_msg_text['state']='disable'
                self.chat_room_root.update()
            except:break
    def close_chat_room(self):
        self.client.close()
        self.chat_room_root.destroy()

def main():
    host='192.168.0.151'
    port=9999
    cc=chat_client(host,port)
    cc.run()
if __name__=='__main__':
    main()